#include "MediaPlayer.h"
#include "ui_MediaPlayer.h"
#include <QPalette>
#include <QMediaPlaylist>
#include <QDebug>
#include <QDir>
#include <QFileDialog>

MediaPlayer::MediaPlayer(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::MediaPlayer)
{
    ui->setupUi(this);

    mPlayer = new QMediaPlayer(this); ///播放器

    mPlayList = new QMediaPlaylist(this); ///播放器列表
    mPlayList->setPlaybackMode(QMediaPlaylist::Loop); ///设置为循环播放模式

    mPlayer->setPlaylist(mPlayList);

    ///设置初始化声音大小
    mPlayer->setVolume(50);
    ui->slider_Volume->setMinimum(0);
    ui->slider_Volume->setMaximum(100);
    ui->slider_Volume->setValue(mPlayer->volume());


    ///关联对应的信号槽大小
    connect (mPlayer, &QMediaPlayer::stateChanged ,this,  &MediaPlayer::onStateChanged);
    connect (mPlayer, &QMediaPlayer::positionChanged, this, &MediaPlayer::onPositionChanged);
    connect (mPlayer, &QMediaPlayer::durationChanged,this, &MediaPlayer::onDurationChanged) ;
    connect (mPlayList, &QMediaPlaylist::currentIndexChanged ,this, &MediaPlayer::onPlaylistChanged) ;
}

MediaPlayer::~MediaPlayer()
{
    delete ui;
}

void MediaPlayer::onStateChanged(QMediaPlayer::State state)
{
    if(mPlayList->isEmpty())
    {
        return;
    }
    ///播放器状态变化，更新按钮状态
    if(state == QMediaPlayer::PlayingState)
    {
        ui->pushButton_play->setIcon(QIcon(":/paly.png"));
    }
    else
    {
        ui->pushButton_play->setIcon(QIcon(":/pause.png"));
    }

}

void MediaPlayer::onPlaylistChanged(int position)
{
    ///播放列表变化,更新当前播放文件名显示
    ui->listWidget->setCurrentRow(position);

    QListWidgetItem * item=ui->listWidget->currentItem() ;
    if (item)
    {
        ui->label_SongName->setText ("正在播放:"+item->text()) ;
    }
}

void MediaPlayer::onDurationChanged(qint64 duration)
{
    ///文件时长变化，更新进度显示
    ui->sliderPosition->setMaximum (duration) ;

    int secs=duration/1000; //秒
    int mins=secs/60; //分钟
    secs=secs%60; //余数秒

    mDurationTime = QString("%1:%2").arg(QString::number(mins), 2, '0').arg(QString::number(secs), 2,  '0');

    ui->label_curPlayTime->setText (mPositionTime+"/"+mDurationTime) ;
}

void MediaPlayer::onPositionChanged(qint64 position)
{
    ///当前文件播放位置变化，更新进度显示.
    if (ui->sliderPosition->isSliderDown()) //正处于 手动调整状态，不处理
        return;

    ui->sliderPosition->setSliderPosition (position) ;
    int secs=position/1000; //秒
    int mins=secs/60; //分钟
    secs=secs%60;//余数秒

    mPositionTime = QString("%1:%2").arg(QString::number(mins),2,'0').arg(QString::number(secs), 2, '0');

    ui->label_curPlayTime->setText (mPositionTime+" /"+mDurationTime) ;
}



void MediaPlayer::on_pushButton_AddSong_clicked()
{
    ///添加文件
    QString curPath=QDir::homePath();//获取用户目录.
    QString dlgTitle= "选择音频文件";
    QString filter="音频文件(* .mp3 *.wav * .wma) ;;mp3文件(* .mp3);;wav文件(* .wav); ;wma文件(*. .wma);;所有文件(*.*)";

    QStringList fileList=QFileDialog::getOpenFileNames (this, dlgTitle,  curPath, filter);

    if (fileList. count() <1)
    {
        return;
    }

    for (int i=0;i<fileList.count() ;i++)
    {
        QString aFile=fileList.at(i) ;
        mPlayList->addMedia(QUrl::fromLocalFile (aFile)); //添加文件
        QFileInfo fileInfo(aFile);

        ui->listWidget->addItem(fileInfo.fileName()) ; //添加到界面文件列表

    }

    if (mPlayer->state() !=QMediaPlayer::PlayingState)
    {
        mPlayList->setCurrentIndex(0) ;
    }

    mPlayer->play() ;
}

void MediaPlayer::on_pushButton_removeSong_clicked()
{
    ///移除文件
    int pos=ui->listWidget->currentRow() ;
    QListWidgetItem *item=ui->listWidget->takeItem(pos) ;
    delete item;  //从listwidget里删除

    mPlayList->removeMedia (pos); //从播 放列表里删除
}

void MediaPlayer::on_pushButton_CleanSong_clicked()
{
    ///清空歌曲
    mPlayList->clear () ;
    ui->listWidget->clear () ;
    mPlayer->stop() ;
}

void MediaPlayer::on_listWidget_doubleClicked(const QModelIndex &index)
{
    ///双击时切换播放文件
    int rowNo=index. row() ;
    mPlayList->setCurrentIndex (rowNo) ;
    mPlayer->play() ;
}

void MediaPlayer::on_pushButton_play_clicked()
{
    if (mPlayList->currentIndex() <0)
    {
        mPlayList->setCurrentIndex (0) ;
    }

    if(mPlayer->state()== QMediaPlayer::PlayingState)
    {
        mPlayer->pause();
    }
    else
    {
         mPlayer->play() ;
    }

}


void MediaPlayer::on_slider_Volume_valueChanged(int value)
{
    ui->label_volumeSize->setText("  "+QString::number(value));
    ///调整音量
    mPlayer->setVolume(value) ;
}

void MediaPlayer::on_sliderPosition_valueChanged(int value)
{
    ///文件进度调控
    mPlayer->setPosition(value);
}

void MediaPlayer::on_pushButton_volumn_clicked()
{
    //静音控制
    bool mute=mPlayer->isMuted() ;
    mPlayer->setMuted(!mute) ;
    if (mute)
    ui->pushButton_volumn->setIcon (QIcon(":/volume.png")) ;
    else
    ui->pushButton_volumn->setIcon (QIcon(":/mute.png")) ;
}
